<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
use DB;
class UserController extends Controller
{
    public function getAdd(){
        return view('admin.user.add');
    }

    public function postInsert(Request $request){
        //验证用户名是否存在
        if(!$request -> has('username')){
            return back() -> withInput() -> with('error_username','请输入用户名');
        }
        if(!$request -> has('password')){
            return back() -> withInput() -> with('error_password','请输入密码');
        }
        if(!$request -> has('repassword')){
            return back() -> withInput() -> with('error_repassword','请再次输入密码');
        }
        
        //自动表单验证
        // $this->validate($request,[
        //     'username' => 'required',
        //     'password' => 'required',
        //     'repassword' => 'required|same:password',
        //     'email' => 'email',
        //     'sex' => 'required'
        // ],[
        //     'username.required' => '请输入用户名',
        //     'password.required' => '请输入密码',
        //     'repassword.required' => '请再次输入密码',
        //     'repassword.same' => '两次输入密码不一致',
        //     'sex.required' => '请选择性别',
        // ]); 
        

        // dd($request);
        //数据组装
        $arr = $request -> only(['username','password','email','sex']);
        $arr['password'] = Hash::make($request -> input('password'));
        //随机一个50字符的token值
        $arr['token'] = str_random(50);
        //格式化当前时间，作为创建时间
        $arr['createtime'] = date('Y-m-d H:i:s',time());
        //插入数据库
        $res = DB::table('tbl_admin_user') -> insert($arr);

        if($res){
            return redirect('/admin/user/index') -> with('success','添加成功');
        }else{
            return back() -> with('error','添加失败');
        }
    }

    public function getIndex(Request $request){
        //获取设定的分页一页信息条数
        $count = $request->input('count',10);
        //获取查询条件
        $search = $request -> input('search','');
        //查询数据库  按照姓名查询  并按照获取数字值进行分页
        $data = DB::table('tbl_admin_user')->where('username','like','%'.$search.'%')->paginate($count);
        //对$request所有的值和数据库查询的结果$data进行传值
        return view('admin.user.index',['data'=>$data,'request'=>$request->all()]);
    }

    public function getDelete($id){
        $res = DB::table('tbl_admin_user') -> where('id','=',$id) -> delete();
        if($res){
            return redirect('/admin/user/index') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }

    public function getEdit($id){
        $data = DB::table('tbl_admin_user') -> where('id','=',$id) -> first(); 
        return view('/admin/user/edit',['data'=>$data]);
    }

    public function postUpdate(Request $request){
        $arr = $request -> except('_token','id');
        $a=$request->input('id');
        $res = DB::table('tbl_admin_user') ->where('id',$request->input('id')) -> update($arr);  
        if($res){
            return redirect('/admin/user/index') -> with('success','修改成功');
        }else{
            return back();
        }
    }

    //前台用户列表展示
    public function getUserlist(Request $request){
        //获取设定的分页一页信息条数
        $count = $request->input('count',10);
        //获取查询条件
        $search = $request -> input('search','');
        //查询数据库  按照姓名查询  并按照获取数字值进行分页
        $data = DB::table('tbl_home_user')->where('username','like','%'.$search.'%')->paginate($count);  
        //对$request所有的值和数据库查询的结果$data进行传值
        return view('admin.user.userlist',['data'=>$data,'request'=>$request->all()]);      
    }

    public function getUseredit($id){
        $data = DB::table('tbl_home_user') -> where('id','=',$id) -> first(); 
        return view('/admin/user/useredit',['data'=>$data]);
    }

     public function postUserupdate(Request $request){
        $arr = $request -> except('_token','id');
        $a=$request->input('id');
        $res = DB::table('tbl_home_user') ->where('id',$request->input('id')) -> update($arr);  
        if($res){
            return redirect('/admin/user/userlist') -> with('success','修改成功');
        }else{
            return back();
        }
    }

    public function getUserdelete($id){
        $res = DB::table('tbl_home_user') -> where('id','=',$id) -> delete();
        if($res){
            return redirect('/admin/user/userlist') -> with('success','删除成功');
        }else{
            return back() -> with('error','删除失败');
        }
    }
}

